<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class BaseRequest extends FormRequest
{

    /**
     * 验证失败处理
     *
     * @param \Illuminate\Contracts\Validation\Validator $validator
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->first();
        // $allErrors = $validator->errors()->all(); 所有错误
        $response = response()->json(
          [
            'state'=>100,
            'msg'=>$error,
            'data'=>[],
            'error'=>$error
          ]
        );

        throw new HttpResponseException($response);
    }
}
